<style lang="less">
  .card_style {
    height: 100%;
    width: 100%;
    overflow: hidden;
    .ivu-card-body {
      padding: 0px;
    }
  }
  .page_style {
    margin-top: 10px;
    text-align: right;
  }
  .bench_search {
    margin-bottom: 10px;
  }
  .nopadding .ivu-table-cell {
    padding: 0px 0px;
  }
  .item1{
    height: 42px;
    line-height: 40px;
  }
  .new-btn-sty {
    margin-right: 5px;
    color: #fff;
    background-color: #235CA7;
    border-color: #235CA7;
    font-weight: 500;
  }
</style>

<template>
  <Card class="card_style" :bordered="false" :dis-hover="true">
    <Row class="bench_search">
      <Col span="5">
        <span>标题：</span>
        <Input style="width: 60%;" v-model="searchInfo.title" />
      </Col>
      <Col span="5">
        <span>社区：</span>
        <Select v-model="searchInfo.company_code" filterable style="width: 60%;">
          <Option v-for="item in communityList" :value="item.company_code" :key="item.id"> {{item.title}}</Option>
        </Select>
      </Col>
      <Col span="5">
        <span>回复类型：</span>
        <Select v-model="searchInfo.type" style="width: 60%;">
          <Option value="">全部</Option>
          <Option value="0">未回复</Option>
          <Option value="1">已回复</Option>
        </Select>
      </Col>
      <Col span="5">
        <span>创建时间：</span>
        <DatePicker type="datetimerange" format="yyyy/MM/dd" transfer="" placement="bottom-end"
                    style="width: 70%" v-model="searchInfo.createtime"></DatePicker>

      </Col>
      <Button class="new-btn-sty" @click="searchData()" style="margin-right:10px"><Icon type="ios-search" /> 搜索</Button>
      <Button class="new-btn-sty" @click="addCheck()"><Icon type="md-add" /> 创建</Button>
      <Modal v-model="modalEdit" title="抽查问题" width="700" cancel-text="关闭"
        @on-ok="ok111(paramData2)" @on-cancel="modalEdit=false" ok-text="确定">
        <Form :model="paramData2" >
          <Row class="item1">
            <Col span="5" class="ivu-form-item-label">社区:</Col>
            <Col span="12" offset="1" class="ivu-form-item-content">
              <Select v-model="paramData2.company_code">
                <Option v-for="item in communityList" :value="item.company_code" :key="item.id"> {{item.title}}</Option>
              </Select>
            </Col>
          </Row>
          <Row class="item1" style="height: 60px; line-height:60px">
            <Col span="5" class="ivu-form-item-label">标题:</Col>
            <Col span="12" offset="1" class="ivu-form-item-content">
              <Input v-model="paramData2.title"></Input>
            </Col>
          </Row>
          <Row class="item1" style="height: 60px; line-height:60px">
            <Col span="5" class="ivu-form-item-label">内容:</Col>
            <Col span="12" offset="1" class="ivu-form-item-content">
              <Input v-model="paramData2.problem" type="textarea" :autosize="{minRows: 2,maxRows: 5}"></Input>
            </Col>
          </Row>
          <Row class="item1">
            <Col span="5" class="ivu-form-item-label">图片:</Col>
            <Col span="4" offset="1" class="ivu-form-item-content">
              <Button @click="uploadImg"><Icon type="ios-add" /> 上传图片</Button>
            </Col>
            <Col span="4" offset="1" class="ivu-form-item-content">
              <Button @click="picPreviewMol.preShow = true"><Icon type="ios-eye-outline" /> 查看</Button>
            </Col>
          </Row>
        </Form>
      </Modal>
      <Modal v-model="modalReply" title="回复详情" width="700" cancel-text="关闭"
        @on-ok="modalReply=false" @on-cancel="modalReply=false" ok-text="确定">
        <Form :model="replyData" >
          <Row class="item1" style="height: 60px; line-height:60px">
            <Col span="5" class="ivu-form-item-label">回复内容:</Col>
            <Col span="12" offset="1" class="ivu-form-item-content">
              <Input v-model="replyData.reply"></Input>
            </Col>
          </Row>
          <Row class="item1">
            <Col span="5" class="ivu-form-item-label">图片:</Col>
            <Col span="4" offset="1" class="ivu-form-item-content">
              <Button @click="picPreviewMol.preShow = true"><Icon type="ios-eye-outline" /> 查看</Button>
            </Col>
          </Row>
        </Form>
      </Modal>
      <Modal v-model="up_pic_mol" title="图片上传" :closable="false">
        <Upload multiple action="http://120.27.63.37:38081/api/ac/file/fileCommonService/upLoadFiles" :before-upload="beforeUpload">
          <Button icon="ios-cloud-upload-outline">上传图片</Button>
          <span style="margin-left: 250px">请上传小于8M的图片</span>
        </Upload>
        <img :src="temp_photo" style="width: 100%"/>
      </Modal>
      <!-- 图片查看模态框 -->
    <Modal v-model="picPreviewMol.preShow" :closable="false" :styles="{top: '20px'}" width="532">
      <Carousel v-model="picPreviewMol.picIndex" style="width:500px" dots="outside" trigger="hover">
        <CarouselItem class="centerimg" v-for="(imginfo, index) in picPreviewMol.pic_urls" :key="index">
          <img :src="imginfo" class="pre_img_style" />
        </CarouselItem>
      </Carousel>
      <div slot="footer" style="text-align: center" v-if="editFlag == false">
        <Button type="info" @click="originalGraph">查看原图</Button>
        <Button type="error" @click="deletePic">删除当前照片</Button>
        <Button @click="picPreviewMol.preShow=false">关闭</Button>
      </div>
    </Modal>
    </Row>
    <Table border :columns="columns1" :data="tempDatalist"></Table>
    <Row v-if="loadData == true">
      <Col class="demo-spin-col" span="8" offset="8">
        <Spin fix>
          <Icon type="ios-loading" size=18 class="demo-spin-icon-load"></Icon>
          <div>数据正在加载</div>
        </Spin>
      </Col>
    </Row>
    <Page :total="totalnum" :current="currentPageNo" :page-size="currentPageSize" @on-change="change" class="page_style"/>
  </Card>
</template>

<script>
import { date2str, getMailLevelList } from '@/api/eventlist'
// import { releaseNoticeList } from '@/api/mainv3'
import { getSign, createUpLoadFiles } from '@/api/common'
import { saveExamine, findExamineList } from '@/api/businessCheck'

export default {
  name: 'businessCheck',
  data () {
    return {
      temp_photo: '',
      // 图片要件预览弹出框控制器
      picPreviewMol: {
        preShow: false,
        pic_urls: [],
        picIndex: 0
      },
      upPicData: {},
      up_pic_mol: false,
      loadData: true,
      showNotice: false,
      modalEdit: false,
      params: { pageNo: 1, pageSize: 10 },
      currentPageSize: 10,
      currentPageNo: 1,
      searchInfo: { title: '', state: '', createtime: [] },
      paramData: {},
      // 修改或添加新通知公告时使用
      paramData2: {},
      totalnum: 0,
      columns1: [
        { title: '序号', type: 'index', width: 70, align: 'center' },
        { title: '标题', key: 'title', width: 300, align: 'center' },
        { title: '问题社区', key: 'company_name', width: 120, align: 'center' },
        {
          title: '类型',
          key: 'type',
          width: 120,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.type === '0' ? '未回复' : '已回复')
          }
        },
        { title: '创建时间', key: 'create_time', width: 250, align: 'center' },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          className: 'nopadding',
          minWidth: 60,
          render: (h, params) => {
            let temp = []
            temp.push(
              h('Button', {
                props: { type: 'info', size: 'small' },
                style: { marginRight: '5px', backgroundColor: '#235CA7', borderColor: '#235CA7' },
                on: {
                  click: () => {
                    this.showInfo(params.row)
                  }
                }
              }, '详情')
              // h('Button', {
              //   props: { type: 'error', size: 'small' },
              //   style: { marginRight: '5px' },
              //   on: {
              //     click: () => {
              //       this.remove(params.row)
              //     }
              //   }
              // }, '删除')
            )
            if (params.row.type === '1') {
              temp.push(
                h('Button', {
                  props: { type: 'success', size: 'small' },
                  style: { marginRight: '5px' },
                  on: {
                    click: () => {
                      this.getReplyInfo(params.row)
                    }
                  }
                }, '回复详情')
              )
            }
            return h('div', temp)
          }
        }
      ],
      datalist: [],
      communityList: [], // 街道下所有社区
      tempDatalist: [],
      replyData: {}, // 回复详情
      modalReply: false,
      editFlag: false
    }
  },
  computed: {
    companyCode () {
      return this.$store.state.user.severUserInfo.company_code
    }
  },
  methods: {
    // 查询订单列表接口
    init (param) {
      this.tempDatalist = []
      findExamineList(param).then(res => {
        if (res.data.errcode === 0) {
          this.datalist = res.data.data.results
          this.totalnum = res.data.data.totalRecord
          for (var i = 0; i < this.datalist.length; i++) {
            this.getCommunityList(this.datalist[i].company_code, i)
          }
          this.loadData = false
        } else {
          this.loadData = true
          this.$Notice.error({ title: '获取业务抽查列表失败', desc: res.data.errmsg })
        }
      })
    },
    ok111 (param) {
      if (this.editFlag === false) {
        this.modalEdit = false
      } else {
        saveExamine(this.paramData2).then(res => {
          if (res.data.errcode === 0) {
            this.init(this.params)
            this.$Notice.success({ title: '成功', desc: '' })
          } else {
            this.$Notice.error({ title: '失败', desc: res.data.errmsg })
          }
        })
      }
    },
    showInfo (item) {
      this.modalEdit = true
      this.editFlag = false
      this.paramData2 = item
      this.picPreviewMol.pic_urls = item.problem_picture.split(',')
    },
    editNotice (item) {
      this.editFlag = true
      this.modalEdit = true
      this.paramData2 = item
    },
    getReplyInfo (item) {
      this.picPreviewMol.pic_urls = []
      this.modalReply = true
      this.replyData = item
      this.editFlag = true
      this.picPreviewMol.pic_urls = this.replyData.reply_picture.split(',')
    },
    remove (item) {
      if (confirm('确定删除该条数据吗')) {
        deleteNotice({ id: item.id }).then(res => {
          if (res.data.errcode === 0) {
            this.init(this.params)
            return
          }
          this.$Notice.error({ title: '删除失败', desc: res.errmsg })
        })
      }
    },
    searchData () {
      let params = {
        state: this.searchInfo.state,
        title: this.searchInfo.title,
        start_time: date2str(this.searchInfo.createtime[0]),
        end_time: date2str(this.searchInfo.createtime[1]),
        company_code: this.searchInfo.company_code,
        type: this.searchInfo.type,
        pageNo: this.currentPageNo,
        pageSize: this.currentPageSize
      }
      this.currentPageNo = 1
      this.init(params)
    },
    change (pageNo) {
      this.currentPageNo = pageNo
      let params = {
        type: this.searchInfo.type,
        pageNo: this.currentPageNo,
        state: this.searchInfo.state,
        title: this.searchInfo.title
      }
      this.init(params)
    },
    // 发布or撤销,state==='1'时表示发布通知，state==='0'时表示撤销通知
    setNoticeState (item, state) {
      let msg = state === '1' ? '确定发布此通知吗！' : '确认撤销此通知吗！'
      var param = { id: item.id, state: state }
      if (confirm(msg)) {
        setReleaseState(param).then(res => {
          if (res.data.errcode === 0) {
            this.init(this.params)
            return
          }
          this.$Notice.error({ title: '失败', desc: res.errmsg })
        })
      }
    },
    getCommunityList (company_code, index) {
      var param = {
        company_code: this.companyCode
      }
      getMailLevelList(param).then(res => {
        if (res.data.errcode === 0) {
          this.communityList = res.data.data
          for (var i = 0; i < this.communityList.length; i++) {
            if (this.communityList[i].company_code === company_code) {
              this.datalist[index]['company_name'] = this.communityList[i].title
              this.tempDatalist.push(this.datalist[index])
            }
          }
          return
        }
        this.$Notice.error({ title: '获取社区列表失败', desc: res.errmsg })
      })
    },
    addCheck () {
      this.paramData2 = {}
      this.modalEdit = true
      this.editFlag = true
    },
    uploadImg () {
      this.up_pic_mol = true
      this.upPicData.fileName = '业务抽查' + this.datetime2str(new Date())
      this.upPicData.filePath = 'businessCheck/' + this.paramData2.company_code
    },
    // 上传图片前转base64
    beforeUpload (file) {
      this.upPicData.fileName = this.upPicData.fileName + '.' + file.type.substring(6)
      let that = this
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = () => {
        let index = reader.result.indexOf('base64,') + 7
        that.upPicData.file = reader.result.substring(index)
        that.upPic(that.upPicData)
      }
      return false
    },
    // 上传图片
    upPic (obj) {
      var time = new Date().getTime()
      this.timeStep = time.toString()
      let param = {
        filePath: obj.filePath,
        fileName: obj.fileName,
        timestamp: this.timeStep
      }
      if (param.fileName.length > 20) {
        param.fileName = param.fileName.substring(0, 20) + '.png'
      }
      getSign(param).then(res => {
        if (res.data.errcode === 0) {
          createUpLoadFiles(res.data.data.docking_url, { sign: res.data.data.sign, timestamp: this.timeStep, filePath: param.filePath, fileName: param.fileName, file: obj.file }).then(res => {
            if (res.data.errcode === 0) {
              let imgurl = res.data.data
              this.picPreviewMol.pic_urls.push(imgurl)
              this.temp_photo = imgurl
              this.paramData2.problem_picture = this.picPreviewMol.pic_urls.join(',')
            } else {
              this.$Notice.error({ title: '上传失败', desc: res.data.data.msg })
            }
          })
        } else {
          this.$Notice.error({ title: '上传失败', desc: res.data.data.msg })
        }
      })
    },
    // 查看图片原图功能
    originalGraph () {
      window.open(this.picPreviewMol.pic_urls[this.picPreviewMol.picIndex])
    },
    // 删除已经上传的图片
    deletePic () {
      this.picPreviewMol.pic_urls.splice(this.picPreviewMol.picIndex, 1)
      if (this.picPreviewMol.pic_urls.length === 0) {
        this.picPreviewMol.preShow = false
      }
    },
    // 时间转字符串 年月日时分秒
    datetime2str (date) {
      let time1 = date.getFullYear() + ''
      let month = date.getMonth() + 1
      let day = date.getDate()
      let hh = date.getHours()
      let mm = date.getMinutes()
      let ss = date.getSeconds()
      time1 += month < 10 ? '0' + month : month
      time1 += day < 10 ? '0' + day : day
      time1 += hh < 10 ? '0' + hh : hh
      time1 += mm < 10 ? '0' + mm : mm
      time1 += ss < 10 ? '0' + ss : ss
      return time1
    }
  },
  mounted () {
    this.init(this.params)
    this.getCommunityList()
  }
}
</script>
